package com.hhp.cloud.shop.admin.controller;

import java.util.Arrays;
import java.util.List;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hhp.cloud.shop.admin.pojo.entity.MenuEntity;
import com.hhp.cloud.shop.admin.service.MenuService;

import com.hhp.cloud.shop.common.core.result.R;



/**
 * 菜单管理
 *
 * @author hhp
 * @email 2437579794@qq.com
 * @date 2021-11-23 10:25:33
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api/v1/admin/menu")
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 列表
     */
    @ApiOperation(value = "菜单列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long"),
            @ApiImplicitParam(name = "name", value = "菜单名称", paramType = "query", dataType = "String")
    })
    @GetMapping
    public R list(Integer page, Integer limit, String name){
        return R.ok().put("data", menuService.pageMaps(
                new Page<>(page, limit),
                new LambdaQueryWrapper<MenuEntity>()
                        .like(StrUtil.isNotBlank(name), MenuEntity::getName, name)));
    }


    /**
     * 信息
     */
    @ApiOperation(value = "菜单详情")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public R info(@PathVariable("id") Long id){
        return R.ok().setData(menuService.getById(id));
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增菜单")
    @PostMapping
    public R save(@RequestBody MenuEntity menu){
		menuService.save(menu);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改菜单")
    @PutMapping("/{id}")
    public R update(@PathVariable("id") Long id, @RequestBody MenuEntity menu){
		menuService.updateById(menu);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除菜单")
    @DeleteMapping
    public R delete(@RequestBody Long[] ids){
		menuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @ApiOperation(value = "菜单路由(Route)列表")
    @GetMapping("/route")
    public R listRouteMenus() {
        return R.ok().setData(menuService.listRouteMenus());
    }
}
